package com.toune.movie.base;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.support.multidex.MultiDexApplication;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.tencent.smtt.sdk.QbSdk;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.service.MyBroadcastReceiver;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.RxTool;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.zhy.autolayout.config.AutoLayoutConifg;

import java.util.List;

/**
 * Created by wangjitao on 2016/11/8 0008.
 */
public class App extends MultiDexApplication {

    private static App instance;

    public static int SCREEN_WIDTH = -1;
    public static int SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE = -1.0F;
    public static int DIMEN_DPI = -1;

    public static synchronized App getInstance() {
        return instance;
    }

    //请求队列
    private static RequestQueue mRequestQueue;

    public static RequestQueue getmRequestQueue() {
        return mRequestQueue;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //屏幕适配初始化
        AutoLayoutConifg.getInstance().useDeviceSize().init(this);
//        ScreenAdapterTools.init(this);
//        getScreenSize();
        //RxTool工具类初始化
        RxTool.init(this);

        //自定义初始化
        NoHttp.initialize(this);
        mRequestQueue = NoHttp.newRequestQueue();
        //TBS 腾讯WebView初始化
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {

            }

            @Override
            public void onViewInitFinished(boolean b) {

            }
        });
        // 初始化参数依次为 this, AppId, AppKey
        AVOSCloud.initialize(this, "sp1f68EbFS5eEQP1rzzdtW3d-gzGzoHsz", "xfyOtzq5xkiQxYwt560z2T5a");

        //获取数据库中的广告
        MyUtil.getADlists();
        AVQuery<AVObject> avQuery = new AVQuery<>(AppConstant.TAB_SPLASH);
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                    if (list != null) {
                        RxSPTool.putString(getInstance(), AppConstant.SPLASH_IMG_URL, list.get(0).getAVFile("file").getUrl());
                        RxSPTool.putString(getInstance(), AppConstant.SPLASH_URL, list.get(0).getString("content"));
                    }
                }
            }
        });

        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        MyBroadcastReceiver receiver = new MyBroadcastReceiver();
        registerReceiver(receiver, filter);
    }

    //旋转适配,如果应用屏幕固定了某个方向不旋转的话(比如qq和微信),下面可不写.
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
//        ScreenAdapterTools.getInstance().reset(this);
    }

    /**
     * 初始化屏幕宽高
     */
    public void getScreenSize() {
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density / 1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT) {
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }

    /**
     * 退出app
     */
    public void exitApp() {
        RxActivityTool.finishAllActivity();
//        android.os.Process.killProcess(android.os.Process.myPid());
//        System.exit(0);
    }

}